package com.hou.rocketmq.controller;

import com.hou.rocketmq.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "测试接口")
@RestController
@RequestMapping("/test/")
public class TestController {

    /**
     * springboot会自动注入,可直接使用
     */
    @Autowired
    RocketMQTemplate rocketMQTemplate;

    @ApiOperation("发送消息")
    @GetMapping("send/{id}")
    public String send(@PathVariable("id") String id){
        //设置自定义发送规则
        rocketMQTemplate.setMessageQueueSelector((mqs,msg,key)->{
            //返回需要自定义发送的队列
            final MessageQueue messageQueue = mqs.get(0);
            return messageQueue;
        });
        rocketMQTemplate.send("test-topic-01",MessageBuilder.withPayload(new User(id,"侯征")).build());
        return "SUCCESS";
    }
}
